# 基类
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support.select import Select


class Base():
    def __init__(self, driver):
        self.driver = driver

    def open_url(self, url):
        self.driver.get(url)

    # 页面元素定位
    def find_element(self, loc):
        return self.driver.find_element(*loc)
    # 输入封装
    def send_keys(self, loc, text):
        self.find_element(loc).send_keys(text)

    # 点击封装
    def click(self, loc):
        self.find_element(loc).click()

    # 键盘事件封装
    def keys_enter(self, loc, key):
        self.find_element(loc).send_keys(key)

    # 下拉菜单封装
    def select_value(self, loc, value):
        city = Select(self.find_element(loc))
        city.select_by_value(value)

    def select_index(self, loc, index):
        city = Select(self.find_element(loc))
        city.select_by_index(index)

    def select_text(self, loc, visible):
        city = Select(self.find_element(loc))
        city.select_by_visible_text(visible)

    # 滚动事件封装
    # 滚动到顶部
    def scrollTo_top(self):
        self.driver.execute_script('window.scrollTo(0,0)')

    # 滚动到底部
    def scrollTo_bush(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollToHeight)')

    # 向下滚动任意元素
    def scrollTo_div(self, s):
        self.driver.execute_script('window.scrollTo(0,{s})')

    # 弹窗
    def alert_accept(self):
        alert = self.driver.switch_to.alert
        alert.accecp()
        alert.dismiss()
        alert.send_keys()

    def alert_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()

    def alert_send_keys_accept(self):
        alert = self.driver.switch_to.alert
        alert.send_keys()
        alert.accecp()

    def alert_send_keys_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.send_keys()
        alert.dismiss()

    # 进入框架
    def switch_to_frame(self, loc):
        self.driver.switch_to.frame(self.find_element(loc))

    # 退出框架
    def default_content(self):
        self.driver.switch_to.default_content()
